<template>
    <div class="app-container">
        <vxe-grid ref="xGrid" v-bind="gridOptions">
            <template #action="{ row }">
                <el-button link type="primary" icon="Delete" @click="handleForceLogout(row)" v-hasPermi="['monitor:online:forceLogout']">强退</el-button>
            </template>
        </vxe-grid>
    </div>
</template>

<script setup lang="ts" name="Online">
    import { ref, getCurrentInstance, reactive } from "vue";
    import { forceLogout, list as initData } from "@/api/monitor/online";
    import { VxeGridInstance, VxeGridProps, VxeGridListeners } from "../../../../types/index";
    import XEUtils from "xe-utils";
    const { proxy } = getCurrentInstance() as any;

    const xGrid = ref({} as VxeGridInstance);
    const gridOptions = reactive<VxeGridProps>({
        border: true,
        resizable: true,
        showHeaderOverflow: true,
        showOverflow: true,
        highlightHoverRow: true,
        keepSource: true,
        id: "11",
        height: "auto",
        rowId: "tokenId",
        sortConfig: {
            trigger: "cell",
            remote: true,
        },
        filterConfig: {
            remote: true,
        },
        pagerConfig: {
            pageSize: 10,
            pageSizes: [5, 10, 15, 20, 50, 100, 200, 500, 1000],
            perfect: true,
        },
        formConfig: {
            titleWidth: 80,
            titleAlign: "right",
            items: [
                {
                    field: "ipaddr",
                    title: "登录地址",
                    span: 4,
                    itemRender: { name: "$input", props: { placeholder: "请输入登录地址" } },
                },
                {
                    field: "userName",
                    title: "用户名称",
                    span: 4,
                    itemRender: { name: "$input", props: { placeholder: "请输入用户名称" } },
                },
                {
                    itemRender: {
                        name: "$buttons",
                        children: [
                            {
                                props: {
                                    type: "submit",
                                    content: "查询",
                                    status: "primary",
                                },
                            },
                            {
                                props: {
                                    type: "reset",
                                    content: "重置",
                                },
                            },
                        ],
                    },
                },
            ],
        },
        toolbarConfig: {
            refresh: true,
            zoom: true,
            custom: true,
        },
        proxyConfig: {
            paramsChangeLoad: false, //额外的请求参数改变时是否重新请求数据
            seq: true, // 启用动态序号代理，每一页的序号会根据当前页数变化
            sort: true, // 启用排序代理，当点击排序时会自动触发 query 行为
            filter: true, // 启用筛选代理，当点击筛选时会自动触发 query 行为
            form: true, // 启用表单代理，当点击表单提交按钮时会自动触发 reload 行为
            // 对应响应结果 { result: [], page: { total: 100 } }
            props: {
                result: "rows", // 配置响应结果列表字段
                total: "total", // 配置响应结果总页数字段
            },
            ajax: {
                // 接收 Promise
                query: ({ page, sorts, filters, form, params }: any) => {
                    const queryParams: any = Object.assign({}, page, form);
                    // 处理排序条件
                    const orderByColumn = <Array<String>>[];
                    const isAsc = <Array<Boolean>>[];
                    sorts.forEach((m: any) => {
                        orderByColumn.push(m.field);
                        isAsc.push(m.order);
                    });
                    queryParams.orderByColumn = orderByColumn.join(",");
                    queryParams.isAsc = isAsc.join(",");
                    // 处理筛选条件
                    filters.forEach(({ property, values }: any) => {
                        queryParams[property] = values.join(",");
                    });
                    return initData({
                        ...queryParams,
                        ...params,
                    }).then((response) => {
                        return response;
                    });
                },
            },
        },
        columns: [
            {
                field: "tokenId",
                title: "会话编号",
            },
            {
                field: "userName",
                title: "登录名称",
            },
            {
                field: "ipaddr",
                title: "主机",
            },
            {
                field: "loginTime",
                title: "登录时间",
                formatter({ cellValue }: any) {
                    return XEUtils.toDateString(cellValue, "yyyy-MM-dd HH:ss:mm");
                },
            },
            {
                title: "操作",
                width: 100,
                align: "center",
                fixed: "right",
                slots: { default: "action" },
            },
        ],
    });

    /** 查询登录日志列表 */
    // function getList() {
    //     loading.value = true;
    //     initData(queryParams.value).then((response: any) => {
    //         onlineList.value = response.rows;
    //         total.value = response.total;
    //         loading.value = false;
    //     });
    // }
    /** 搜索按钮操作 */
    // function handleQuery() {
    //     current.value = 1;
    //     getList();
    // }
    /** 重置按钮操作 */
    // function resetQuery() {
    //     proxy.resetForm("queryRef");
    //     handleQuery();
    // }
    /** 强退按钮操作 */
    function handleForceLogout(row: any) {
        proxy.$modal
            .confirm('是否确认强退名称为"' + row.userName + '"的用户?')
            .then(function () {
                return forceLogout(row.tokenId);
            })
            .then(() => {
                xGrid.value.commitProxy("reload");
                proxy.$modal.msgSuccess("删除成功");
            })
            .catch(() => {});
    }
</script>
